package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;

import common.cache.CacheManager;
import common.cache.CacheMode;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.*;

@Entity
public class Article extends Model  {
	 private static final long serialVersionUID = 19981017L;
	public String title;
	@Lob
	public String desc;
	@Lob
	public String body;
	public Date date;

	public void saveArticle(String title, String desc, String body, Date date) {
		this.title = title;
		this.desc = desc;
		this.body = body;
		this.date = date;
		
		save();
	}
	
	public static Article  getArticle(CacheMode cacheMode ,long id){
		Article article = null;
		article = (Article)CacheManager.getCacheItem("article"+id, cacheMode);
		if(article == null){
			article = Article.findById(id);
			if(article != null){
				CacheManager.setCacheItem("article"+id, article, cacheMode);
			}
		}
		return article;
		
	}
	public static Article findOrCreateArticleByTitle(String title){
		Article article = Article.find("title=?",title ).first();
		if(article == null){
			article = new Article();
			article.title = title;
		}
		return article;
	}
	
	public static List<Article> getArticleOrderByDate(){
		String jpaQuery = "SELECT art FROM Article art ORDER BY  art.date DESC";
		Query query = JPA.em().createQuery(jpaQuery);
		List<Article> articleList = query.getResultList();
		return articleList;
	}
	
	public List<Tag> getTags(){
		String jpaQuery = "SELECT tg FROM Tag tg ,ArticleTag atg WHERE tg.id = atg.tagID and atg.articleID = :id";
		Query query = JPA.em().createQuery(jpaQuery);
		query.setParameter("id",getId());
		return query.getResultList();
	}
	
	public List<Tag> getTags(CacheMode cacheMode){
		List<Tag> tagList = null;
		tagList = (List<Tag>) CacheManager.getCacheItem("article"+this.getId()+"tags", cacheMode);
		if(tagList == null){
			tagList = getTags();
			
			CacheManager.setCacheItem("article"+this.getId()+"tags", tagList, cacheMode);
			
		}
		return tagList;
	}
	
	public static List<Article> getArticleOrderByDate(int nextOffset){
		String jpaQuery = "SELECT art FROM Article art  ORDER BY  art.date DESC ";
		Query query = JPA.em().createQuery(jpaQuery);
		query.setFirstResult(nextOffset);
		query.setMaxResults(30);
		List<Article> articleList = query.getResultList();
		return articleList;
	}
	
	public static List<Article> getArticleOrderByDate(CacheMode cacheMode ,int nextOffset){
		   List<Article> articleList = (List<Article>)CacheManager.getCacheItem("articleList1"+nextOffset, cacheMode);
		   if(articleList == null){
			
			   articleList = getArticleOrderByDate(nextOffset); 
			   if(nextOffset == 0){
			     CacheManager.setCacheItem("articleList1"+nextOffset, articleList, cacheMode);
			   }else{
				   CacheManager.setCacheItem("articleList1"+nextOffset,articleList, 1000, cacheMode);
			   }
		   }
		   return articleList;
	}

	
	


}